const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentDate: new Date().getTime(),
    minDate:  new Date(1949,8,31).getTime(),
    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`;
      }
      if (type === 'month') {
        return `${value}月`;
      }
      if(type === 'day'){
        return `${value}日`;
      }
      return value;
    },
    fieldListShow:false,
    endBool:false,
    startBool:false,
    fieldValue:'项目预付',
    type:'',
    amount:'',
    id:'',
    typeArray:[
      {
        name:"项目预付",
        value:0,
      },
      {
        name:"工作阶段",
        value:1,
      },
      {
        name:"项目售后",
        value:2,
      }
    ],
    typeIndex:0,
    startDate:'',
    endDate:'',
    programName:'',
    programDesc:'',
    programProportion:'',
    payAmount:'',
    editData:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.type == 'add'){
      this.setData({
        type:options.type,
        amount:options.amount,
        id:options.id
      })
    }else{
      let editData = JSON.parse(options.obj)
      this.setData({
        type:options.type,
        amount:options.amount,
        id:editData.task_id,
        editData,
        typeIndex:parseInt(editData.type) - 1,
        programName:editData.name,
        programProportion:editData.percent,
        startDate:editData.start_time,
        endDate:editData.end_time,
        programDesc:editData.desc,
        payAmount:editData.price
      })
    }
  },

  startCancel(){
    this.setData({
      startBool:false,
    })
  },

  endCancel(){
    this.setData({
      endBool:false,
    })
  },


  industryClick(){
    this.setData({
      fieldListShow:true,
    })
  },

  onCloseReportList(){
    this.setData({
      fieldListShow:false,
    })
  },


  onSelectReportList(event){
    this.setData({
      typeIndex:event.detail.value,
      fieldValue:event.detail.name,
    })
  },

  startBoolClick(){
    this.setData({
      startBool:true,
    })
  },

  endBoolClick(){
    this.setData({
      endBool:true,
    })
  },

  startHand(e){
    this.setData({
      startBool:false,
      startDate: this.formatDate(e.detail)
    })
  },

  endHand(e){
    this.setData({
      endBool:false,
      endDate: this.formatDate(e.detail)
    })
  },

  formatDate: function (value) {//调用时间戳为日期显示
    let date = new Date(value)
    let y = date.getFullYear()  //获取年份
    let m = date.getMonth() + 1  //获取月份
    m = m < 10 ? "0" + m : m  //月份不满10天显示前加0
    let d = date.getDate()  //获取日期
    d = d < 10 ? "0" + d : d  //日期不满10天显示前加0
    return y + "-" + m + "-" + d
  },

  taskDescriptionTap(){
    wx.navigateTo({
      url: '/userPage/pages/storeText/storeText?type=4&text='+this.data.programDesc,
    })
  },


  // 阶段类型
  programChange: function(e) {
    this.setData({
      typeIndex: e.detail.value
    })
  },
  // 开始/结束时间
  startDateChange: function(e) {
    this.setData({
      startDate: e.detail.value
    })
  },
  endDateChange: function(e) {
    this.setData({
      endDate: e.detail.value
    })
  },
  // 说明
  programDescFun(e){
    let {value} = e.detail
    this.setData({
      programDesc:value
    })
  },
  // 阶段名称
  programNameFun(e){
    let {value} = e.detail
    this.setData({
      programName:value
    })
  },
  // 付款比例
  programProportionFun(e){
    let value= e.detail
    let {amount,payAmount} = this.data
    if(value > 100){
      value = 100
      this.setData({
        programProportion:value
      })
    }
    payAmount = amount * value / 100
    this.setData({
      programProportion:value,
      payAmount
    })
  },
  // 保存
  saveBtn(){
    var url = app.globalData.baseUrl + 'myTask/addPaysection';
    var { id,typeIndex,startDate,endDate,programName,programDesc,programProportion,payAmount,editData } = this.data;
    var editId = ''
    if(editData){
      editId = editData.id
    }
    var data = {
      task_id: id,
      token: app.globalData.token,
      desc:programDesc,
      percent:programProportion,
      start_time:startDate,
      end_time:endDate,
      type:parseInt(typeIndex) + 1,
      name:programName,
      price:payAmount,
      id:editId
    }
    wx.showLoading({
      title: '加载中',
    })
    app.wxPostRequest(url, data,(res) =>{
      wx.hideLoading()
      if (res.code == 1000){
        wx.showToast({
          title: '保存成功',
          icon: 'success',
          duration: 1000
        })
        setTimeout(function(){
          wx.navigateBack({
            delta: 1
          })
        },1000)
      }else{
        wx.showToast({
          title: res.message,
          icon: 'none',
          duration: 2000
        })
      }
    }, (err) =>{
      wx.hideLoading()
      wx.showToast({
        title: err.data.message,
        icon: 'none',
        duration: 2000
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
